package com.htgd.gardenexpo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.htgd.gardenexpo.entity.*;
import com.htgd.gardenexpo.service.GeConCarouselService;
import com.htgd.gardenexpo.utils.SecurityParameter;
import com.htgd.gardenexpo.utils.UniteUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：hejing
 * @date ：Created in 2021/5/24 17:20
 * @description：巨幕管理
 * @menu: 巨幕管理
 * @modified By：`
 * @version: 1.0
 */
@Api(tags = "巨幕管理")
@RestController
@RequestMapping("/api/giantscreen")
public class GiantScreenController {

    @Resource
    private GeConCarouselService geConCarouselService;


    /**
     * 新增巨幕
     */
    @PostMapping(value = "/add")
    public CommonResult<JSONObject> add(@RequestBody GeConCarousel geConCarousel){
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            geConCarouselService.insert(geConCarousel);
            result.setCode(200);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    /**
     * 编辑巨幕
     */
    @PostMapping(value = "/edit")
    public CommonResult<JSONObject> edit(@RequestBody GeConCarousel geConCarousel){
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            geConCarouselService.update(geConCarousel);
            result.setCode(200);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    /**
     * 删除巨幕
     */
    @PostMapping(value = "/delete")
    public CommonResult<JSONObject> delete(int carouselId){
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            geConCarouselService.deleteById(carouselId);
            result.setCode(200);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    /**
     * 批量删除巨幕
     */
//    @PostMapping(value = "/deleteAll")
    public CommonResult<JSONObject> deleteAll(String carouselId){
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            if (carouselId!=null&&!carouselId.equals("")){
                for (String promo:carouselId.split(",")){
                    int promoId= Integer.parseInt(promo);
                    delete(promoId);
                }
            }

            result.setCode(200);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    /**
     * 历史巨幕检索列表
     */
    @PostMapping(value = "/list")
    @SecurityParameter
    public CommonResult<List<GeConCarousel>> list(@RequestBody PageQuery pageQuery){
        CommonResult<List<GeConCarousel>> result = new CommonResult<>();
        try {
            pageQuery.setPageNum((pageQuery.getPageNum()-1)* pageQuery.getPageSize());
            List<GeConCarousel> list=geConCarouselService.getAllCarouselByValue(pageQuery);
            if (list != null && list.size() > 1) {
                for (GeConCarousel geConCarousel : list) {
                    if(geConCarousel.getIntroduction()!=null){
                        String content= UniteUtils.stripHtml(geConCarousel.getIntroduction().toString());
                        if (!content.equals("")){
                            geConCarousel.setIntroduction(content.substring(0,content.length()>200?200:content.length()));
                        }else {
                            geConCarousel.setIntroduction("");
                        }
                    }
                }
            }
            pageQuery.setPageSize(0);
            List<GeConCarousel> list1=geConCarouselService.getAllCarouselByValue(pageQuery);
            result.setCode(200);
            result.setData(list);
            result.setMessage(String.valueOf(list1.size()));
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @PostMapping(value = "/homeList")
    @ApiOperation("小程序首页")
    @SecurityParameter
    public CommonResult<List<GeConCarousel>> homeList(@RequestBody PageQuery pageQuery){
        CommonResult<List<GeConCarousel>> result = new CommonResult<>();
        List<GeConCarousel> data = new ArrayList<>();
        List<GeConCarousel> data1 = new ArrayList<>();
        try {
            pageQuery.setPageNum((pageQuery.getPageNum()-1)* pageQuery.getPageSize());
            List<GeConCarousel> list=geConCarouselService.getAllCarouselByValue(pageQuery);
            if (list != null && list.size() > 1) {
                for (GeConCarousel geConCarousel : list) {
                    if(geConCarousel.getIntroduction()!=null){
                        String content= UniteUtils.stripHtml(geConCarousel.getIntroduction().toString());
                        if (!content.equals("")){
                            geConCarousel.setIntroduction(content.substring(0,content.length()>200?200:content.length()));
                        }else {
                            geConCarousel.setIntroduction("");
                        }
                    }
                    //分类
                    if(geConCarousel.getType() == 1){
                        data.add(geConCarousel);
                    }else {
                        data1.add(geConCarousel);
                    }
                }
            }
            pageQuery.setPageSize(0);
            List<GeConCarousel> list1=geConCarouselService.getAllCarouselByValue(pageQuery);
            result.setCode(200);
            result.setData(data);
            result.setData1(data1);
            result.setMessage(String.valueOf(list1.size()));
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    /**
     * 巨幕发布
     */
    @GetMapping(value = "/release")
    public CommonResult<JSONObject> release(){
        CommonResult<JSONObject> result = new CommonResult<>();
        return result;
    }

    /**
     * 巨幕取消发布
     */
    @GetMapping(value = "/unpublish")
    public CommonResult<JSONObject> unpublish(){
        CommonResult<JSONObject> result = new CommonResult<>();
        return result;
    }

    /**
     * 巨幕排序
     */
    @GetMapping(value = "/sort")
    public CommonResult<List<Map<String,Object>>> sort(){
        CommonResult<List<Map<String, Object>>> result = new CommonResult<>();
        return result;
    }

    /**
     * 巨幕置顶
     */
    @GetMapping(value = "/top")
    public CommonResult<JSONObject> top(){
        CommonResult<JSONObject> result = new CommonResult<>();
        return result;
    }


    /**
     * 巨幕审核
     */
    @GetMapping(value = "/review")
    public CommonResult<JSONObject> review(GeConCarousel geConCarousel){
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            GeConCarousel geConCarousel1=geConCarouselService.queryById(geConCarousel.getCarouselId());
            geConCarousel1.setStatus(geConCarousel.getStatus());
            geConCarouselService.update(geConCarousel1);
            result.setCode(200);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    /**
     * 巨幕图片链接
     */
    @GetMapping(value = "/link")
    public CommonResult<JSONObject> link(){
        CommonResult<JSONObject> result = new CommonResult<>();
        return result;
    }

    /**
     * 巨幕详情
     */
    @PostMapping(value = "/detail")
    @SecurityParameter
    public CommonResult<GeConCarousel> detail(@RequestBody String s){
        CommonResult<GeConCarousel> result = new CommonResult<>();
        try{
            Integer carouselId= JSON.parseObject(s).getInteger("carouselId");
            GeConCarousel geConCarousel=geConCarouselService.queryById(carouselId);
            result.setCode(200);
            result.setData(geConCarousel);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }


}
